<?php

/*
	Class: Blog_RssController

	About: Author
		Rich Joslin

	About: License
		<http://communit.as/docs/license>
*/
class Blog_RssController extends Cts_Controller_Action_Abstract {

	function indexAction() {
	}

	function feedAction() {
		$request = new Cts_Request($this->getRequest());
		$posts_table = new BlogPosts();
		if ($request->has('name')) {
			$feeds_table = new BlogRssFeeds();
			$feed = $feeds_table->fetchRowArray($feeds_table->select()->where("name = ?", $request->name));
			$tags = unserialize($feed['tags']);
			$this->getPostsByTags($tags);
		} elseif ($request->has('tags')) {
			$this->getPostsByTags(explode(',', trim(urldecode($request->tags), ',')));
		} else {
			$this->getAllPosts();
		}
	}

	function customizeAction() {
		$request = new Cts_Request($this->getRequest());
		$tag_index_table = new BlogRssTagsPopularity();
		$pop_tags_data = $tag_index_table->fetchAllArray(null, 'popularity desc', Cts_Registry::get('rss_popular_tags_count', 'blog'));
		$pop_tags = array();
		foreach ($pop_tags_data as $tag_data) {
			$pop_tags[] = $tag_data['tag'];
		}
		$this->view->pop_tags = $pop_tags;
		$this->view->tags = "";
		if ($this->_request->isPost()) {
			$this->view->tags = $request->tags;
			$tags = explode(',', $request->tags);
			switch ($request->submit) {
				case 'Preview':
					$this->getPostsByTags($tags);
					break;
				default:
					$tags = serialize($tags);
					$guid = uniqid();
					$data = array(
						'name' => $guid,
						'tags' => $tags,
						'created_on' => date(DB_DATETIME_FORMAT),
						'last_modified_on' => date(DB_DATETIME_FORMAT),
					);
					$feeds_table = new BlogRssFeeds();
		            $feeds_table->insert($data);
					$this->reindexTagPopularity();
					$this->_redirect('/blog/rss/done/feed_name/'.$guid);
					break;
			}
		}
		$this->view->feature_photo = Blackdiamond_Common::getBgImage(Cts_Registry::get('custom_rss_pageparts_page_id', 'pageparts'));
	}

	function doneAction() {
		$request = new Cts_Request($this->getRequest());
		if (!$request->has('feed_name')) {
			$this->_redirect('auth/missing');
		} else {
			$urlfilter_params = array('locale_code' => $this->locale_code);
			$url_filter = new Cts_Url_Filter();
			$this->view->feed_url = $url_filter->filter("/blog/rss/feed/name/".$request->feed_name, $urlfilter_params);
		}
	}

	private function getPostsByTags($tags) {
		$posts_table = new BlogPosts();
		$tmp_posts = $posts_table->fetchAllByTags($tags,false,Cts_Registry::get('rss_feed_items','blog'));
		$this->getPostsAsRss($tmp_posts);
	}

	private function getAllPosts() {
		$posts_table = new BlogPosts();
		$tmp_posts = $posts_table->fetchAllActive();
		$this->getPostsAsRss($tmp_posts);
	}

	private function getPostsAsRss($tmp_posts) {
		$blogcats_table = new BlogCategories();
		if (!is_null($tmp_posts)) {
			$tmp_posts = $tmp_posts->toArray();
			$posts = array();
			foreach ($tmp_posts as $post) {
				$post['title'] = strip_tags(stripslashes($post['title']));
				$post['body'] = strip_tags(stripslashes($post['body']),"<a><p><br>" );				
				$cats = $blogcats_table->getParsedSlugsByPostId($post['id']);
				$post['url'] = "/journal/".$cats[0]['activity']."/".$cats[0]['subsection']."/".$post['slug'];
				$posts[] = $post;
			}
			
			$this->view->posts = $posts;
		}
	}

	private function reindexTagPopularity() {
		$feeds_table = new BlogRssFeeds();
		$tag_index_table = new BlogRssTagsPopularity();
		$feed_rows = $feeds_table->fetchAll();
		$tag_counts = array();
		if (!is_null($feed_rows)) {
			$feed_arrays = $feed_rows->toArray();
			foreach ($feed_arrays as $feed_array) {
				$more_tags = unserialize($feed_array['tags']);
				foreach ($more_tags as $tag) {
					if (trim($tag) != '') {
						if (array_key_exists($tag, $tag_counts)) {
							$tag_counts[$tag]++;
						} else {
							$tag_counts[$tag] = 1;
						}
					}
				}
			}
		}
		$tag_index_table->delete();
		foreach ($tag_counts as $key => $value) {
			$data = array(
				'tag' => $key,
				'popularity' => $value,
				'created_on' => date(DB_DATETIME_FORMAT),
				'last_modified_on' => date(DB_DATETIME_FORMAT),
			);
			$tag_index_table->insert($data);
		}
	}

}
